#! /usr/bin/python
# -*- coding: utf-8 -*-
import re
import sys
import time
import lsoutput
class lsdo:
    def format(self, string):
        """添加任务，任务/写入时间/标签/执行时间/执行地点"""
        todolist = {}
        todolist['data'] = re.findall(r'^.+?(?=\s\W)', string) #任务
        ISOTIMEFORM = '%Y-%m-%d %X'
        todolist['time'] = time.strftime(ISOTIMEFORM, time.localtime()) #写入时间
        todolist['tag'] = re.findall(r'(?<=\#).+?(?=\s)', string) #标签
        todolist['priority'] = len(re.findall(r'(\!)', string)) #重要性
        todolist['startTime'] = re.findall(r'(?<=\^).+?(?=[\s]|$)', string) #执行时间
        todolist['adr'] = re.findall(r'(?<=\@).+?(?=\s)', string) #执行地点
        todolist['finish'] = False #是否完成
        return todolist
    def output(self, data, format="xml"):
        output_function = getattr(lsoutput, "output_%s" % format)
        return output_function(data)
class lsdo_cli(lsdo):
    def pr(self, dict):
        return '%(priority)s %(data)s @%(adr)s #%(tag)s' %dict
if __name__=="__main__":
    if len(sys.argv) < 2:
        print 'No action specified.'
        sys.exit()
    if sys.argv[1] == 'add':
        todo = lsdo_cli()
        x = todo.format(sys.argv[2])
        print 'sourse -->', sys.argv[2]
        print 'format -->', todo.pr(x)
     
